﻿// Copyright (c) Matthias Koch
//
// See the NOTICE file distributed with this work for additional information
// regarding copyright ownership.  rubicon licenses this file to you under 
// the Apache License, Version 2.0 (the "License"); you may not use this 
// file except in compliance with the License.  You may obtain a copy of the 
// License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the 
// License for the specific language governing permissions and limitations
// under the License.
// 

using System;
using Rhino.Mocks;
using Xunit;

namespace XValidation.UnitTests
{
  public class InterceptionValidatorTest
  {
    public class ValidateArguments
    {
      private readonly IValidation _argumentValidation;
      private readonly IValidation _returnValidation;

      private readonly ValidationItem _argumentItem;
      private readonly ValidationItem _returnItem;

      private readonly IValidationInfoProvider _infoProvider;

      public ValidateArguments ()
      {
        _infoProvider = MockRepository.GenerateMock<IValidationInfoProvider>();
        _argumentValidation = MockRepository.GenerateMock<IValidation>();
        _returnValidation = MockRepository.GenerateMock<IValidation>();

        _argumentItem = new ValidationItem
                        {
                            ParameterName = "arg",
                            ArgumentsIndex = 0,
                            Validations = new[] { _argumentValidation }
                        };
        _returnItem = new ValidationItem
                      {
                          ParameterName = "return",
                          ArgumentsIndex = -1,
                          Validations = new[] { _returnValidation }
                      };
      }

      [Fact]
      public void CallsValidations ()
      {
        _infoProvider.Expect (x => x.GetValidationItems (null))
            .Return (new[] { _argumentItem });
        var validator = new InterceptionValidator (_infoProvider, null);
        var context = MockRepository.GenerateMock<IValidationContext> ();
        var value = 1;
        context.Expect (x => x.Arguments).Return (new object[] { value });

        validator.ValidateArguments (context);

        _argumentValidation.AssertWasCalled (x => x.Validate (value, _argumentItem.ParameterName, context));
      }

      [Fact]
      public void DoesntCallReturnValidations ()
      {
        _infoProvider.Expect (x => x.GetValidationItems (null))
            .Return (new[] { _returnItem });
        var validator = new InterceptionValidator (_infoProvider, null);
        var context = MockRepository.GenerateMock<IValidationContext> ();
        context.Expect (x => x.Arguments).Return (new object[0]);

        validator.ValidateArguments (context);

        _returnValidation.VerifyAllExpectations();
      }
    }
  }
}